package cn.stronglink.asset.module.assetmanage.scrap.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.Scrap;
import cn.stronglink.asset.module.assetmanage.itasset.service.ItassetService;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.scrap.service.ScrapService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.util.WebUtil;

@Controller
@RequestMapping(value = "scrap")
public class ScrapController extends AbstractController {

	@Autowired
	private ScrapService scrapService;
	@Autowired
	private ItassetService itassetService;

	// 查询所有报废单
	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryScrapList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {	
		boolean isAdmin = (boolean) request.getSession().getAttribute("isAdmin");
		if(!isAdmin) {
			params.put("operator_by", WebUtil.getCurrentUser());
		}
		@SuppressWarnings("unchecked")
		Page<Scrap> page = (Page<Scrap>) super.getPage(params);
		Page<Scrap> data = scrapService.queryScrapList(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	// 根据报废单Id查询明细
	@RequestMapping(value = "qryById")
	public ResponseEntity<ModelMap> queryScrapDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Long id) {	
		Scrap list =scrapService.queryScrapById(id);
		List<ItAssetVo> vo = itassetService.qryScrapDetailVo(id);
		modelMap.put("dataVo", vo);
		return setSuccessModelMap(modelMap, list);
	}
	
	/**
	 * 新增报废单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param Scrap
	 * @return
	 */
	@OperateLog(module = "报废单管理",desc="添加报废单", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Scrap scrap) {
		scrapService.create(scrap);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 编辑领用详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@OperateLog(module = "报废单管理",desc="维修报废单", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Scrap scrap) {
		scrapService.edit(scrap);
		return setSuccessModelMap(modelMap, null);
	}
	/**
	 * 删除领用详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "报废单管理",desc="删除报废单", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		scrapService.remove(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 执行报废
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@OperateLog(module = "报废单管理",desc="确认报废", type = OpType.UPDATE)
	@RequestMapping(value = "doScrap")
	public ResponseEntity<ModelMap> doScrap(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Scrap scrap) {
		scrapService.doScrap(scrap);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 所在机房,设备类型,品牌,设备名称模糊查找资产
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "querAssetListByParam")
	public ResponseEntity<ModelMap> queryDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {	
		@SuppressWarnings("unchecked")
		Page<ItAssetVo> page = (Page<ItAssetVo>) super.getPage(params);
		Page<ItAssetVo> data = scrapService.querAssetListByParam(page,params);
		return setSuccessModelMap(modelMap, data);
	}
}
